class RoomsController < ApplicationController
	# GET /rooms
	# GET /rooms.xml
	def index
		@rooms = Room.find(:all, :order => 'name')
		@events_temp = Room.find(:all,
							:joins => "	RIGHT JOIN computers ON rooms.id = computers.room_id
										LEFT JOIN events ON computers.id = events.computer_id",
							:from => "rooms",
							:select => "rooms.id AS room_id, count(events.id) AS count",
							:conditions => "events.state = 1 OR computers.state_computer = 0 OR computers.state_agent = 0",
							:group => "rooms.id"
						)

		@riddance = {:id => 0, :name => "Stock"}
		@riddance_events_temp = Room.find(:all,
							:joins => "	RIGHT JOIN computers ON rooms.id = computers.room_id
										LEFT JOIN events ON computers.id = events.computer_id",
							:from => "rooms",
							:select => "rooms.id AS room_id, count(events.id) AS count",
							:conditions => "(rooms.id = 0 OR rooms.id IS NULL) AND (events.state = 0 OR computers.state_computer = 0 OR computers.state_agent = 0)",
							:group => "rooms.id"
							)
		
		@events = []

		@events_temp.each do | event |
			unless @events.include?(event.room_id)
				
				@events << event.room_id.to_i

			end
		end
		
		@riddance_events = []

		@riddance_events_temp.each do | event |
			unless @riddance_events.include?(event.room_id)
				
				@riddance_events << event.room_id.to_i

			end
		end

		# Stats
		events_count_sql = TypeEvent.find(:all,
											:joins => "INNER JOIN events ON type_events.id = events.type_event_id
													  INNER JOIN computers ON computers.id = events.computer_id
													  INNER JOIN rooms ON rooms.id = computers.room_id",
											:from => "type_events",
											:order => "events.created_at DESC",
											:select => "rooms.name as room_name, type_events.name as type_event, computers.name as computer_name, computers.id as computer_id, events.id as event_id, events.updated_at as date_resolution, events.created_at as date_creation, events.information as event_info, events.state as state_event "
											)
		@events_count = events_count_sql.count

		computers_count_sql = Computer.all
		@computers_count = computers_count_sql.count

		respond_to do |format|
			format.html # index.html.erb
			format.xml  { render :xml => @rooms }
		end
	end

	# GET /rooms/1
	# GET /rooms/1.xml
	def show
		if params[:id].to_i > 0
			begin
				@room = Room.find(params[:id])
				@computers = Computer.find_all_by_room_id(params[:id])
				@events_temp = Computer.find_all_by_room_id(params[:id],
									:joins => "	LEFT JOIN events ON computers.id = events.computer_id",
									:select => "computers.id AS computer_id, count(events.id) AS count",
									:conditions => "events.state = 0 OR computers.state_computer = 0 OR computers.state_agent = 0",
									:group => "computers.id"
								)
			rescue
				@error = true;
			end
			

		else
			room_tab = {:id => 0, :name => "Stock"}
			@room = Room.new(room_tab)
			@computers = Computer.find(:all,
										:conditions => "room_id = 0 OR room_id IS NULL")
			@events_temp = Computer.find_all_by_room_id(0,
										:joins => "	LEFT JOIN events ON computers.id = events.computer_id",
										:select => "computers.id AS computer_id, count(events.id) AS count",
										:conditions => "(computers.room_id = 0 OR computers.room_id IS NULL) AND (events.state = 0 OR computers.state_computer = 0 OR computers.state_agent = 0)",
										:group => "computers.id"
							)
		end
				
		# if no error
		if !@error			
			@events = []

			@events_temp.each do | event |
				unless @events.include?(event.computer_id)
					
					@events << event.computer_id.to_i

				end
			end
			@room = Room.find(params[:id])
			@computers = Computer.find_all_by_room_id(params[:id])
		end

		respond_to do |format|
			format.html # show.html.erb
			format.xml  { render :xml => @room }
			format.js { }
		end
	end

	# GET /rooms/new
	# GET /rooms/new.xml
	def new
		@room = Room.new

		respond_to do |format|
			format.html # new.html.erb
			format.xml  { render :xml => @room }
		end
	end

	# GET /rooms/1/edit
	def edit
		if params[:id].to_i > 0
			begin
				@room = Room.find(params[:id])
				@id = @room.id
			rescue
				@error = true
			end
		else
			@riddance = true
			@id = 0
		end

	end

	# POST /rooms
	# POST /rooms.xml
	def create
		@room = Room.new(params[:room])

		respond_to do |format|
			if @room.save
				flash[:notice] = 'La salle a bien été créée.'
				format.html { redirect_to(root_url) }
				format.xml  { render :xml => @room, :status => :created, :location => @room }
			else
				format.html { render :action => "new" }
				format.xml  { render :xml => @room.errors, :status => :unprocessable_entity }
			end
		end
	end

	# PUT /rooms/1
	# PUT /rooms/1.xml
	def update
		@room = Room.find(params[:id])
	

		respond_to do |format|
			if @room.update_attributes(params[:room])
				flash[:notice] = 'La salle a bien été mise à jour.'
				format.html { redirect_to(root_url) }
				format.xml  { head :ok }
			else
				format.html { render :action => "edit" }
				format.xml  { render :xml => @room.errors, :status => :unprocessable_entity }
			end
		end
	end

	# DELETE /rooms/1
	# DELETE /rooms/1.xml
	def destroy
		@room = Room.find(params[:id])
		@room.destroy

		respond_to do |format|
			format.html { redirect_to(rooms_url) }
			format.xml  { head :ok }
		end
	end

	# GET ROOM
	def get_room
		if params[:id].to_i > 0
			@room = Room.find(params[:id])
			@computers = Computer.find_all_by_room_id(params[:id])
			@events_temp = Computer.find_all_by_room_id(params[:id],
								:joins => "	LEFT JOIN events ON computers.id = events.computer_id",
								:select => "computers.id AS computer_id, count(events.id) AS count",
								:conditions => "events.state = 1 OR computers.state_computer = 0 OR computers.state_agent = 0",
								:group => "computers.id"
							)

		else
			room_tab = {:id => 0, :name => "Stock"}
			@room = Room.new(room_tab)
			@computers = Computer.find(:all,
										:conditions => "room_id = 0 OR room_id IS NULL")
			@events_temp = Computer.find_all_by_room_id(0,
										:joins => "	LEFT JOIN events ON computers.id = events.computer_id",
										:select => "computers.id AS computer_id, count(events.id) AS count",
										:conditions => "(computers.room_id = 0 OR computers.room_id IS NULL) AND (events.state = 0 OR computers.state_computer = 0 OR computers.state_agent = 0)",
										:group => "computers.id"
							)
		end
							
		@events = []

		@events_temp.each do | event |
			unless @events.include?(event.computer_id)
				
				@events << event.computer_id.to_i

			end
		end

		respond_to do |format|
			format.html
			format.js {
				render :partial => 'get_room'
			}
		end
	end

	# Check if exist event in room
	def check_exist_events
		@room = Room.find(params[:id])

		respond_to do |format|
			format.html {  	 }
			format.xml  { head :ok }
			format.json {
				
			}
		end
	end
end

